package cn.wolfcode.education.service.impl;

import cn.wolfcode.education.domain.LossStu;
import cn.wolfcode.education.domain.RegularStu;
import cn.wolfcode.education.mapper.LossStuMapper;
import cn.wolfcode.education.mapper.RegularStuMapper;
import cn.wolfcode.education.query.QueryObject;
import cn.wolfcode.education.service.ILossStuService;
import cn.wolfcode.education.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LossStuServiceImpl implements ILossStuService {
    @Autowired
    private LossStuMapper lossStuMapper;
    @Autowired
    private RegularStuMapper regularStuMapper;

    public void insert(LossStu record) {
        lossStuMapper.insert(record);
    }

    public void update(LossStu record) {
        //获取正式学员表中的该学员
        Long regularStuId = record.getRegularStuId();
        RegularStu regularStu = regularStuMapper.selectByPrimaryKey(regularStuId);
        //计算修改后的退学时间和之前的升班时间的差值
        record.setInClassDays(String.valueOf((record.getRemoveStuTime().getTime() - (regularStu.getClassPromoteDate()).getTime()) / (1000 * 60 * 60 * 24)));
        lossStuMapper.updateByPrimaryKey(record);
    }

    public LossStu selectByPrimaryKey(Long id) {
        return lossStuMapper.selectByPrimaryKey(id);
    }

    public List<LossStu> selectAll() {
        return lossStuMapper.selectAll();
    }

    public PageResult query(QueryObject qo) {
        Integer total = lossStuMapper.queryForCount(qo);
        if(total == 0){
            return new PageResult();
        }
        List<LossStu> rows = lossStuMapper.queryForList(qo);
        return new PageResult(total,rows);
    }

    public void auditLossStu(Long id) {
        lossStuMapper.auditLossStu(id);
    }

    public void unauditLossStu(Long id) {
        lossStuMapper.unauditLossStu(id);
    }
}
